package com.graduation.rescuematerial.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.rescuematerial.mapper.HelpNewsMapper;
import com.graduation.rescuematerial.pojo.HelpNews;
import com.graduation.rescuematerial.service.IHelpNewsService;
import com.graduation.rescuematerial.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CaiShuangLian
 * @since 2022-04-24
 */
@Service
public class HelpNewsServiceImpl extends ServiceImpl<HelpNewsMapper, HelpNews> implements IHelpNewsService {

    @Autowired HelpNewsMapper helpNewsMapper;

    /**
     * 发布信息
     * @param helpNewsVo
     * @return
     */
    @Override
    public RespBean publishHelpInfo(HelpNewsVo helpNewsVo,Long phone,String companyName) {

        HelpNewsInfo helpNewsInfo=new HelpNewsInfo();
//        helpNewsInfo.setHelpNewsVo(helpNewsVo);
        helpNewsInfo.setAddress(helpNewsVo.getAddress());
        helpNewsInfo.setPhone(helpNewsVo.getPhone());
        helpNewsInfo.setName(helpNewsVo.getName());
        helpNewsInfo.setDescription(helpNewsVo.getDescription());
        helpNewsInfo.setKeyword(helpNewsVo.getKeyword());

        helpNewsInfo.setCompanyName(companyName);
        helpNewsInfo.setPublishPhone(phone);


        int row=helpNewsMapper.publishHelpInfo(helpNewsInfo);//发布求助信息
        if(row<=0){
            return RespBean.error(RespBeanEnum.PUBLISH_ERROR);
        }
        return RespBean.success("发布成功！");
    }

    @Override
    public RespBean getAll(String type) {
        if(type.equals("all")){
            return RespBean.success(helpNewsMapper.getAll());
        }
        return RespBean.success(helpNewsMapper.getUnsolveInfo());

    }

    @Override
    public RespBean getByCompanyName(String companyName) {
        return RespBean.success(helpNewsMapper.getByCompanyName(companyName));
    }

    @Override
    public RespBean updateStatus(UpdateHelpNewsVo updateHelpNewsVo) {

        if(updateHelpNewsVo.getStatus().equals(0)){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"该问题已解决!不可再次修改");
        }

        int row=helpNewsMapper.updateStatusByCompanyName(updateHelpNewsVo);
        if(row<=0){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"修改失败");
        }
        return RespBean.success("修改成功！");
    }

    @Override
    public RespBean fuzzyQuery(String key) {
        return RespBean.success(helpNewsMapper.fuzzyQuery(key));
    }
}
